﻿namespace QMVnSoft.Controls.ChildGridControls
{
    using QMVnSoft;
    using QMVnSoft.Controls;
    using System;
    using System.ComponentModel;
    using System.Windows.Forms;

    public class NumericCell : DataGridViewTextBoxCell
    {
        private bool ib_AllowNegative = true;
        private bool ib_CheckMovable = true;
        private bool ib_ZeroIsShown;
        private bool ib_ZeroIsValid;
        private AppInstance.TextNumericFormat ie_TextNumericFormat;
        private int ii_Precision = 1;
        private int ii_ScaleOnFocus;
        private int ii_ScaleOnLostFocus;

        public override object Clone()
        {
            NumericCell cell = base.Clone() as NumericCell;
            if (cell != null)
            {
                cell.AllowNegative = this.AllowNegative;
                cell.NumericPrecision = this.NumericPrecision;
                cell.NumericScaleOnFocus = this.NumericScaleOnFocus;
                cell.NumericScaleOnLostFocus = this.NumericScaleOnLostFocus;
                cell.ZeroIsValid = this.ZeroIsValid;
                cell.UserTextNumericFormat = this.UserTextNumericFormat;
                cell.ZeroIsShown = this.ZeroIsShown;
                cell.CheckMovable = this.CheckMovable;
            }
            return cell;
        }

        protected override object GetFormattedValue(object value, int rowIndex, ref DataGridViewCellStyle cellStyle, TypeConverter valueTypeConverter, TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context)
        {
            object obj2 = base.GetFormattedValue(value, rowIndex, ref cellStyle, valueTypeConverter, formattedValueTypeConverter, context);
            string str = obj2 as string;
            if (string.IsNullOrEmpty(str) || (value == null))
            {
                return obj2;
            }
            decimal num = Convert.ToDecimal(value);
            decimal num2 = Convert.ToDecimal(str);
            if (!(num == num2))
            {
                return obj2;
            }
            if (!this.ZeroIsShown && !(num != 0M))
            {
                return "";
            }
            return num2.ToString("N" + this.NumericScaleOnLostFocus.ToString());
        }

        public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
        {
            base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
            TextNumeric editingControl = base.DataGridView.EditingControl as TextNumeric;
            if (editingControl != null)
            {
                editingControl.BorderStyle = BorderStyle.None;
                editingControl.AllowNegative = this.AllowNegative;
                editingControl.NumericPrecision = this.NumericPrecision;
                editingControl.NumericScaleOnFocus = this.NumericScaleOnFocus;
                editingControl.NumericScaleOnLostFocus = this.NumericScaleOnLostFocus;
                editingControl.ZeroIsValid = this.ZeroIsValid;
                editingControl.UserTextNumericFormat = this.UserTextNumericFormat;
                editingControl.ZeroIsShown = this.ZeroIsShown;
                editingControl.CheckMovable = this.CheckMovable;
                string str = initialFormattedValue as string;
                if (str == null)
                {
                    editingControl.Text = string.Empty;
                }
                else
                {
                    editingControl.Text = str;
                }
            }
        }

        private void OnCommonChange()
        {
            if (((base.DataGridView != null) && !base.DataGridView.IsDisposed) && !base.DataGridView.Disposing)
            {
                if (base.RowIndex == -1)
                {
                    base.DataGridView.InvalidateColumn(base.ColumnIndex);
                }
                else
                {
                    base.DataGridView.UpdateCellValue(base.ColumnIndex, base.RowIndex);
                }
            }
        }

        private bool OwnsEditingNumericControl(int rowIndex)
        {
            if ((rowIndex == -1) || (base.DataGridView == null))
            {
                return false;
            }
            NumericEditing editingControl = base.DataGridView.EditingControl as NumericEditing;
            return ((editingControl != null) && (rowIndex == editingControl.EditingControlRowIndex));
        }

        internal void SetAllowNegative(int rowIndex, bool value)
        {
            this.ib_AllowNegative = value;
            if (this.OwnsEditingNumericControl(rowIndex))
            {
                this.EditingNumericControl.AllowNegative = value;
            }
        }

        internal void SetCheckMovable(int rowIndex, bool value)
        {
            this.ib_CheckMovable = value;
            if (this.OwnsEditingNumericControl(rowIndex))
            {
                this.EditingNumericControl.CheckMovable = value;
            }
        }

        internal void SetNumericPrecision(int rowIndex, int value)
        {
            this.ii_Precision = value;
            if (this.OwnsEditingNumericControl(rowIndex))
            {
                this.EditingNumericControl.NumericPrecision = value;
            }
        }

        internal void SetNumericScaleOnFocus(int rowIndex, int value)
        {
            this.ii_ScaleOnFocus = value;
            if (this.OwnsEditingNumericControl(rowIndex))
            {
                this.EditingNumericControl.NumericScaleOnFocus = value;
            }
        }

        internal void SetNumericScaleOnLostFocus(int rowIndex, int value)
        {
            this.ii_ScaleOnLostFocus = value;
            if (this.OwnsEditingNumericControl(rowIndex))
            {
                this.EditingNumericControl.NumericScaleOnLostFocus = value;
            }
        }

        internal void SetUserTextNumericFormat(int rowIndex, AppInstance.TextNumericFormat value)
        {
            this.ie_TextNumericFormat = value;
            if (this.OwnsEditingNumericControl(rowIndex))
            {
                this.EditingNumericControl.UserTextNumericFormat = value;
            }
        }

        internal void SetZeroIsShown(int rowIndex, bool value)
        {
            this.ib_ZeroIsShown = value;
            if (this.OwnsEditingNumericControl(rowIndex))
            {
                this.EditingNumericControl.ZeroIsShown = value;
            }
        }

        internal void SetZeroIsValid(int rowIndex, bool value)
        {
            this.ib_ZeroIsValid = value;
            if (this.OwnsEditingNumericControl(rowIndex))
            {
                this.EditingNumericControl.ZeroIsValid = value;
            }
        }

        public bool AllowNegative
        {
            get
            {
                return this.ib_AllowNegative;
            }
            set
            {
                if (this.ib_AllowNegative != value)
                {
                    this.SetAllowNegative(base.RowIndex, value);
                    this.OnCommonChange();
                }
            }
        }

        public bool CheckMovable
        {
            get
            {
                return this.ib_CheckMovable;
            }
            set
            {
                if (this.ib_CheckMovable != value)
                {
                    this.SetCheckMovable(base.RowIndex, value);
                    this.OnCommonChange();
                }
            }
        }

        private NumericEditing EditingNumericControl
        {
            get
            {
                return (base.DataGridView.EditingControl as NumericEditing);
            }
        }

        public override System.Type EditType
        {
            get
            {
                return typeof(NumericEditing);
            }
        }

        public int NumericPrecision
        {
            get
            {
                return this.ii_Precision;
            }
            set
            {
                if (this.ii_Precision != value)
                {
                    this.SetNumericPrecision(base.RowIndex, value);
                    this.OnCommonChange();
                }
            }
        }

        public int NumericScaleOnFocus
        {
            get
            {
                return this.ii_ScaleOnFocus;
            }
            set
            {
                if (this.ii_ScaleOnFocus != value)
                {
                    this.SetNumericScaleOnFocus(base.RowIndex, value);
                    this.OnCommonChange();
                }
            }
        }

        public int NumericScaleOnLostFocus
        {
            get
            {
                return this.ii_ScaleOnLostFocus;
            }
            set
            {
                if (this.ii_ScaleOnLostFocus != value)
                {
                    this.SetNumericScaleOnLostFocus(base.RowIndex, value);
                    this.OnCommonChange();
                }
            }
        }

        [DefaultValue(0)]
        public AppInstance.TextNumericFormat UserTextNumericFormat
        {
            get
            {
                return this.ie_TextNumericFormat;
            }
            set
            {
                if (this.ie_TextNumericFormat != value)
                {
                    this.SetUserTextNumericFormat(base.RowIndex, value);
                    this.OnCommonChange();
                }
            }
        }

        public override System.Type ValueType
        {
            get
            {
                return typeof(decimal);
            }
        }

        public bool ZeroIsShown
        {
            get
            {
                return this.ib_ZeroIsShown;
            }
            set
            {
                if (this.ib_ZeroIsShown != value)
                {
                    this.SetZeroIsShown(base.RowIndex, value);
                    this.OnCommonChange();
                }
            }
        }

        public bool ZeroIsValid
        {
            get
            {
                return this.ib_ZeroIsValid;
            }
            set
            {
                if (this.ib_ZeroIsValid != value)
                {
                    this.SetZeroIsValid(base.RowIndex, value);
                    this.OnCommonChange();
                }
            }
        }
    }
}

